package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 服务结算单控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "/business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
/*
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }
*/

    //数据-----------------------------------------------------------
    //列表按钮 列表分页数据
    @RequiresPermissions("business:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo,Long statementId){
        qo.setStatementId(statementId);
        return statementItemService.query(qo);
    }


    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String itemDetail(Model model, Long statementId){
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        // 当状态为消费中时, 点击明细跳转到明细编辑页面
        if( Statement.STATUS_CONSUME.equals(statement.getStatus())){
            return prefix +"editDetail";
        }else{
            // 当状态为已支付时, 点击明细 跳转到服务明细页面
            Long payeeId = statement.getPayeeId();
            User payee = userService.get(payeeId);
            statement.setPayee(payee);
            return prefix +"showDetail";
        }
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    // @RequestBody 将前台传入的数据注入
    public AjaxResult saveItems(@RequestBody()List<StatementItem> items){
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        statementService.payStatement(statementId);
        return AjaxResult.success();
    }




}
